package com.kuaiying.kuai.ying.model.leaflet;

import java.util.Iterator;
import java.util.List;

import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.kuaiying.kuai.ying.model.admin.AdminModel;
import com.kuaiying.kuai.ying.model.print_file.PrintfileModel;
import com.kuaiying.kuai.ying.model.task_id.TaskidModel;

public class LeafletModel extends Model<LeafletModel>{

	private static final long serialVersionUID = -6984293973714614241L;
	
	public static final LeafletModel me = new LeafletModel();
	
	private static final String USER_CACHE = "leaflet";
	
	public LeafletModel(){
		super( );
	}
	/**
	 * 所有 sql 与业务逻辑写在 Model 或 Service 中，不要写在 Controller 中，养成好习惯，有利于大型项目的开发与维护
	 */
	public Page<LeafletModel> paginate(int pageNumber, int pageSize) {
		return paginate(pageNumber, pageSize, "select *", "from  leaflet order by id asc");
	}
	
	//分页测试
	public List<LeafletModel> getAllLeaftel_shopage(Integer shop_id,int pageNumber){
		Page<LeafletModel> Page_LeafletModel = paginate(pageNumber, 10, "select *", "from  leaflet where shop_id = "+ Integer.toString(shop_id) + " order by leaflet_id asc");
		List<LeafletModel> leaflets  = Page_LeafletModel.getList();
		return leaflets;
	}
	
	/**
	 * 查找当天每个传单情况
	 * @param userName：用戶名
	 * @param pwd：用戶密碼
	 * @return：用戶信息或null
	 */
	public List<LeafletModel> getAllLeaftel_shop(Integer shop_id){
		System.out.println( "====================");
		String ss= Integer.toString(shop_id);
		String sql = "select * from leaflet where shop_id = "+Integer.toString(shop_id);
		System.out.println( sql );
		List<LeafletModel> leaflets  = me.find(sql);
		return leaflets;
	}
	
	public List<LeafletModel> getAllLeaftel(){
		String sql = "select * from leaflet";
		System.out.println( sql );
		List<LeafletModel> leaflets  = me.find(sql);
		return leaflets;
	}
	
	//用户查找他没有用过的传单
	public List<LeafletModel> getAllLeaftel_nofinish(List<Integer> finish_leaflets,String school,Integer needgold){
		String finish_ids_str = " where (school = 0 or school = "+ school+") and remainder_money >= " + needgold.toString()+ " and leaflet_id not in (";
		boolean head_flag = true;
		System.out.println( finish_leaflets );
		for(Iterator i = finish_leaflets.iterator();i.hasNext(); ){
			Object value = i.next();
			if (value != null){
				if (head_flag){
					head_flag = false;
				}
				else{
					finish_ids_str += ",";
				}
				String finish_id_str = value.toString();
	            finish_ids_str += finish_id_str;
			}
		}
		finish_ids_str += ")";
		String sql = "select * from leaflet" + finish_ids_str;
		System.out.println( "==========" );
		System.out.println( sql );
		System.out.println( "==========" );
		List<LeafletModel> leaflets  = me.find(sql);
		return leaflets;
	}
	
	public boolean updateleafletfile(Integer shop_id,String filename, String path,String now_time,Integer gold_num,Integer school_id,Integer remainder_money){
		LeafletModel shop = new LeafletModel();
		shop.set("name", filename);
		shop.set("upload_time", now_time);
		shop.set("shop_id", shop_id);
		shop.set("path", path);
		shop.set("user_num", 0);
		shop.set("gold_num", 1000);
		shop.set("use_gold", 0);
		shop.set("paper_num", 0);
		shop.set("gold_num", gold_num);
		shop.set("remainder_money", remainder_money);
		shop.set("school", school_id);
		return shop.save();
	}

	
}


